﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WindbgTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Add(list);

            //for (int i = 0; i < 50; i++)
            //{
            //    Task.Factory.StartNew(() =>
            //        {
            //            LockTest();
            //        });
            //}
            // string fileName = string.Empty;
            //fileName = GetMd5("http://img1.guokr.com/thumbnail/GSeRdZZrMGzRJLO7b-RLfUyoOpOYKqFA5VMlxqTWz4KgAAAAoAAAAEpQ_48x48.jpg");
            //Console.WriteLine(fileName);
            //fileName = GetMd5("http://www.guokr.com/post/540097/?page=2");
            //Console.WriteLine(fileName);

            //Stopwatch watch = new Stopwatch();
            //watch.Start();
            //int count = 0;
            //for (int i = 0; i < 10000000; i++)
            //{
            //    Interlocked.Add(ref count, 3);
            //}
            //Console.WriteLine(watch.ElapsedMilliseconds);
            //watch.Reset();
            //watch.Restart();
            //for (int i = 0; i < 10000000; i++)
            //{
            //    count += 3;
            //}
            //Console.WriteLine(watch.ElapsedMilliseconds);

            //var table = new DataTable("StaticClass");
            //table.Columns.Add(new DataColumn("Age",typeof(object)));
            //table.Columns.Add(new DataColumn("Name", typeof(object)));
            //var row = table.NewRow();
            //row["Age"] = 132;
            //row["Name"] = 132;
            //table.Rows.Add(row);
            //var l = table.ToList<StaticClass>();
            //Console.WriteLine(l);

           
            

           Console.ReadKey();
        }

        private static string GetMd5(string sre)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(sre));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
        }


        private static void ObjectCloneTest()
        {
            var user = new User()
            {
                Age = 123,
                Name = "test case",
                U2 = new U2() { Name = "U2" }
            };

            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 100000; i++)
            {
                using (Stream objectStream = new MemoryStream())
                {
                    //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制  
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(objectStream, user);
                    objectStream.Seek(0, SeekOrigin.Begin);
                    formatter.Deserialize(objectStream);
                }
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
            watch.Reset();
            watch.Start();
            for (int i = 0; i < 100000; i++)
            {
                using (Stream stream = new MemoryStream())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(User));
                    serializer.Serialize(stream, user);
                    stream.Seek(0, SeekOrigin.Begin);
                    serializer.Deserialize(stream);
                }
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

            var user2 = (User)user.Clone();

            Console.WriteLine(user.Equals(user2));

            user2.U2.Name = "new";
        }

        private static void Add(List<User> list)
        {
            Console.WriteLine(DateTime.Now);
            for (int i = 0; i < 100; i++)
            {
                list.Add(new User() { Name = "name" + i, Age = i * i });
            }
            long l = 2400000000L;
            while (l > 0)
            {
                l--;
            }
            Console.WriteLine(DateTime.Now);
        }
    }

    [Serializable]
    public struct User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public U2 U2 { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
    [Serializable]
    public class U2
    {
        public string Name { get; set; }
    }
}
